package com.ruoyi.web.controller.store;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.RestaurantStoreResourceMove;
import com.ruoyi.common.core.domain.entity.StoreIn;
import com.ruoyi.common.core.domain.entity.StoreOut;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.mapper.RestaurantStoreResourceMapper;
import com.ruoyi.system.mapper.StoreInMapper;
import com.ruoyi.system.mapper.StoreOutMapper;
import com.ruoyi.system.service.IRestaurantStoreResourceMoveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 食材数量变动明细记录Controller
 *
 * @author wlmes
 * @date 2023-11-24
 */
@RestController
@RequestMapping("/system/move")
@Api(tags = "食材数量变动明细记录相关接口")
public class RestaurantStoreResourceMoveController extends BaseController {
    @Autowired
    private IRestaurantStoreResourceMoveService restaurantStoreResourceMoveService;
    @Resource
    RestaurantStoreResourceMapper storeResourceMapper;
    @Resource
    StoreOutMapper storeOutMapper;
    @Resource
    StoreInMapper storeInMapper;

    /**
     * 查询食材数量变动明细记录列表
     */
    @GetMapping("/list")
    @ApiOperation("查询食材数量变动明细记录列表")
    public TableDataInfo list(RestaurantStoreResourceMove restaurantStoreResourceMove) {
        startPage();
        List<RestaurantStoreResourceMove> list = restaurantStoreResourceMoveService.selectRestaurantStoreResourceMoveList(restaurantStoreResourceMove);
        for (RestaurantStoreResourceMove move : list) {
            String name = storeResourceMapper.getName(move.getResourceId());
            move.setName(name);
            move.setResourceName(name);

            StoreOut search = new StoreOut();
            search.setId(Long.valueOf(move.getBusinessNum()));
            search.setType(move.getBusinessType());
            List<StoreOut> storeOut = storeOutMapper.selectList(search);
            if (!CollectionUtils.isEmpty(storeOut)) {
                move.setUpdateTime(storeOut.get(0).getOutTime());
            } else {
                StoreIn in = new StoreIn();
                in.setId(Long.valueOf(move.getBusinessNum()));
                in.setType(move.getBusinessType());
                List<StoreIn> storeIn = storeInMapper.selectList(in);
                if (!CollectionUtils.isEmpty(storeIn)) {
                    move.setUpdateTime(storeIn.get(0).getInTime());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出食材数量变动明细记录列表
     */
//    @PreAuthorize("@ss.hasPermi('system:move:export')")
    @Log(title = "食材数量变动明细记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出食材数量变动明细记录列表")
    public AjaxResult export(RestaurantStoreResourceMove restaurantStoreResourceMove) {
        List<RestaurantStoreResourceMove> list = restaurantStoreResourceMoveService.selectRestaurantStoreResourceMoveList(restaurantStoreResourceMove);
        ExcelUtil<RestaurantStoreResourceMove> util = new ExcelUtil<RestaurantStoreResourceMove>(RestaurantStoreResourceMove.class);
        return util.exportExcel(list, "move");
    }

    /**
     * 获取食材数量变动明细记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:move:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取食材数量变动明细记录详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(restaurantStoreResourceMoveService.selectRestaurantStoreResourceMoveById(id));

    }

    /**
     * 新增食材数量变动明细记录
     */
//    @PreAuthorize("@ss.hasPermi('system:move:add')")
    @Log(title = "食材数量变动明细记录", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增食材数量变动明细记录")
    @RepeatSubmit
    public AjaxResult add(@RequestBody RestaurantStoreResourceMove restaurantStoreResourceMove) {
        return toAjax(restaurantStoreResourceMoveService.insertRestaurantStoreResourceMove(restaurantStoreResourceMove));
    }

    /**
     * 修改食材数量变动明细记录
     */
//    @PreAuthorize("@ss.hasPermi('system:move:edit')")
    @Log(title = "食材数量变动明细记录", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改食材数量变动明细记录")
    @RepeatSubmit
    public AjaxResult edit(@RequestBody RestaurantStoreResourceMove restaurantStoreResourceMove) {
        return toAjax(restaurantStoreResourceMoveService.updateRestaurantStoreResourceMove(restaurantStoreResourceMove));
    }

    /**
     * 删除食材数量变动明细记录
     */
//    @PreAuthorize("@ss.hasPermi('system:move:remove')")
    @Log(title = "食材数量变动明细记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除食材数量变动明细记录")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(restaurantStoreResourceMoveService.deleteRestaurantStoreResourceMoveByIds(ids));
    }
}
